package pl.un4giv3n.mightvsmagic.action.clan;

import java.util.List;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.entity.Clan;
import pl.un4giv3n.mightvsmagic.entity.ClanWar;
import pl.un4giv3n.mightvsmagic.utils.ClanUtil;

public class ClanWarPageAction extends ClanAction{

	private static final long serialVersionUID = 1L;
	private List<ClanWar> currentWars;
	private List<ClanWar> oldWars;
	private Long clanId;

	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		if(clanId == null){
			clan = user.getClan();
		}else{
			clan = entityManager.find(Clan.class, clanId);
		}
		if(clan == null){
			addActionError("Wybrany klan nie istnieje");
			entityManager.close();
			return LOGIN;
		}
		currentWars = ClanUtil.getCurrentClanWars(clan, entityManager);
		oldWars = ClanUtil.getOldClanWars(clan, entityManager);
		return SUCCESS;
	}

	public List<ClanWar> getCurrentWars() {
		return currentWars;
	}

	public void setCurrentWars(List<ClanWar> currentWars) {
		this.currentWars = currentWars;
	}

	public List<ClanWar> getOldWars() {
		return oldWars;
	}

	public void setOldWars(List<ClanWar> oldWars) {
		this.oldWars = oldWars;
	}

	public Long getClanId() {
		return clanId;
	}

	public void setClanId(Long clanId) {
		this.clanId = clanId;
	}

}
